|
|
|
|
|
|
|
and string operations, each database supports many operations and functions unique to that database. For example, Microsoft Access allows you to use the same set of functions as Visual Basic, such as MID$ or UCASE$. The following SQL statement shows how to get the average sale made for the contact as part of the query: |
|
|
|
|
|
|
|
|
SELECT Contact.[First Name], Contact.[Last Name], Sales.*,
Sales.[Sale Total] / Sales.[Sale Units] as [Avg Sale]
FROM Contact, Sales WHERE Contact.ID = Sales.ID |
|
|
|
|
|
|
|
|
Although this SQL statement will create a recordset that you can update, any calculated field in the recordset is read-only. |
|
|
|
|
|
|
|
|
Changing the Tables Names |
|
|
|
|
|
|
|
|
Of the names of the tables you're using, the SQL statement could get very long when supplying the table names in the fieldlist. To prevent this from happening and to simplify the SQL code, you can assign a short name to any table in the statement, much the same was that you can rename a field in the fieldlist. By using aliases in the FROM clause, you can assign a name to each table that makes sense to you. For example, the preceding SQL statement could be rewritten as follows: |
|
|
|
|
|
|
|
|
SELECT CT.[First Name], CT.[Last Name], SA.*,
SA.[Sale Total] / SA.[Sale Units] as [Avg Sale]
FROM Contact as CT, Sales as SA WHERE CT.ID = SA.ID |
|
|
|
|
|
|
|
|
As you can see, this approach makes the SQL statement a little easier to read. |
|
|
|
|
|
|
|
|
One of the more powerful features of SQL is its capability to control the range of records to be processed by specifying a filter condition. You can use many types of filters, such as [Last name] = Jones, Units > 1, or [Order Date] between #5/1/94# and #5/31/94#. |
|
|
|
|
|
|
|
|
Note Although the SELECT statement is being reviewed, the basics of filtering can be used in other SQL statements, such as DELETE and UPDATE. |
|
|
|
|
|
|
|
|
|
Filter conditions in a SQL command are specified in the WHERE clause. The syntax of the WHERE clause is as follows: |
|
|
|
|
|